package com.xxgc.cloud.error.center;

import com.xxgc.cloud.error.center.exception.TokenInvalidException;
import com.xxgc.cloud.error.center.exception.TokenNullException;
import com.xxgc.cloud.error.center.message.IBaseMsgException;
import com.xxgc.cloud.error.center.result.ErrResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: SpringCloudStudy
 * @description: 全局异常拦截
 * @author: Mr.abel(ShiJiaYi)
 * @create: 2022-11-23 14:29
 **/
@Slf4j
@ControllerAdvice
@ResponseBody
public class GlobalException implements IBaseMsgException {
    //后期去配置中心设置
    @Value("${spring.profiles.active}")
    protected String active = "prod";//当前环境
    protected String system = "服务器压力过大，请稍后重试！";
    protected String myActive = "prod";//什么环境下隐藏BUG

    //token异常
    @ExceptionHandler(value = TokenInvalidException.class)
    public ErrResult<TokenInvalidException> tokenInvalidException(TokenInvalidException e){
        log.error(e.toString());
        String msg = active.equals(myActive) ? system : e.getDefaultMessage();
        return active.equals(myActive) ? ErrResult.error(msg,e.getCode()) : ErrResult.error(msg,e.getCode(),e);
    }

    @ExceptionHandler(value = TokenNullException.class)
    public ErrResult<TokenNullException> tokenNullException(TokenNullException e){
        log.error(e.toString());
        String msg = active.equals(myActive) ? system : e.getDefaultMessage();
        return active.equals(myActive) ? ErrResult.error(msg,e.getCode()) : ErrResult.error(msg,e.getCode(),e);
    }
}
